#include<iostream>
#include<cstring>
#include<algorithm>
#include<assert.h>
using namespace std;

namespace STL
{
	class String
	{
	public:
		typedef char* iterator;
		typedef const char* const_iterator;

		//默认成员函数
		String(const char* str = "")//构造函数
		{
			_size = strlen(str);
			_capacity = _size;
			_str = new char[_capacity + 1];
			strcpy(_str, str);
		}
		String(const String& s)//拷贝构造函数
			:_str(new char[s._capacity + 1]),
			_size(0),
			_capacity(0)
		{
			strcpy(_str, s._str);
			_size = s._size;
			_capacity = s._capacity;
		}
		String& operator=(const String& s)//赋值运算符重载函数
		{
			if (this != &s)
			{
				delete[] _str;
				_str = new char[s._capacity + 1];
				strcpy(_str, s._str);
				_size = s._size;
				_capacity = s._capacity;
			}
			return *this;
		}
		~String()	//析构函数
		{
			delete[]_str;
			_str = nullptr;
			_size = 0;
			_capacity = 0;
		}

		//迭代器相关函数
		iterator begin()
		{
			return _str;
		}
		iterator end()
		{
			return _str + _size;
		}
		const_iterator begin()const
		{
			return _str;
		}
		const_iterator end()const
		{
			return _str + _size;
		}

		//容量和大小相关的函数
		size_t size()
		{
			return _size;
		}
		size_t capacity()
		{
			return _capacity;
		}
		void reserve(size_t n)
		{
			if (n > _capacity)
			{
				char* tmp = new char[n + 1];
				strncpy(tmp, _str, _size + 1);
				delete[]_str;
				_str = tmp;
				_capacity = n;
			}
		}
		void resize(size_t n, char ch = '\0')
		{
			if (n <= _size)
			{
				_size = n;
				_str[_size] = '\0';
			}
			else
			{
				if (n > _capacity)
				{
					reserve(n);
				}
				for (size_t i = _size; i < n; ++i)
				{
					_str[i] = ch;
				}
				_size = n;
				_str[_size] = '\0';
			}
		}
		bool empty()
		{
			return size() == 0;
		}

		//修改字符串相关函数
		void push_back(char ch)
		{
			insert(_size, ch);
		}
		void append(const char* str)
		{
			insert(_size, str);
		}
		String& operator+=(char ch)
		{
			insert(_size, ch);
			return *this;
		}
		String& operator+=(const char* str)
		{
			insert(_size, str);
			return *this;
		}
		String& insert(size_t pos, char ch)
		{
			assert(pos<=_size);
			if (_size == _capacity)
			{
				reserve(_capacity == 0 ? 4 : _capacity * 2);
			}
			char* end = _str + _size;
			while (end >=_str+ pos)
			{
				*(end+1) = *end;
				end--;
			}
			_str[pos] = ch;
			_size++;
			return *this;
		}
		String& insert(size_t pos, const char* str)
		{
			assert(pos <= _size);
			size_t len = strlen(str);
			if (_size + len > _capacity)
			{
				reserve(_size + len);
			}
			char* end = _str+_size;
			while (end >= _str + pos)
			{
				*(end + len) = *end;
				end--;
			}
			strncpy(_str + pos, str, len);
			_size += len;
			return *this;
		}
		String& erase(size_t pos, size_t len)
		{
			assert(pos < _size);
			size_t n = _size - pos;
			if (len >= n)
			{
				_size = pos;
				_str[_size] = '\0';
			}
			else
			{
				strcpy(_str + pos, _str + pos + len);
				_size -= len;
			}
			return *this;
			
		}
		void clear()
		{
			_size = 0;
			_str[_size] = '\0';
		}
		void swap(String& s)
		{
			::swap(_str, s._str);
			::swap(_size, s._size);
			::swap(_capacity, s._capacity);
		}
		const char* c_str()const
		{
			return _str;
		}

		//访问字符串相关函数
		char& operator[](size_t i)
		{
			assert(i < _size);
			return _str[i];
		}
		const char& operator[](size_t i)const
		{
			assert(i < _size);
			return _str[i];
		}
		size_t find(char ch, size_t pos = 0)const
		{
			assert(pos < _size);
			for (size_t i = pos; i < _size; ++i)
			{
				if (_str[i] == ch)
				{
					return i;
				}
			}
			return npos;
		}
		size_t find(const char* str, size_t pos = 0)const
		{
			assert(pos < _size);
			const char* ret = strstr(_str + pos, str);
			if (ret)
			{
				return ret - _str;
			}
			else
			{
				return npos;
			}
		}
		size_t rfind(char ch, size_t pos = npos)const
		{
			String tmp(*this);
			reverse(tmp.begin(), tmp.end());
			if (pos >= _size)
			{
				pos = _size - 1;
			}
			pos = _size - 1 - pos;
			size_t ret = tmp.find(ch, pos);
			if (ret != npos)
			{
				return _size - 1 - ret;
			}
			else
			{
				return npos;
			}
		}
		size_t rfind(const char* str, size_t pos = 0)const
		{
			String tmp(*this); //拷贝构造对象tmp
			reverse(tmp.begin(), tmp.end()); //调用reverse逆置对象tmp的C字符串
			size_t len = strlen(str); //待查找的字符串的长度
			char* arr = new char[len + 1]; //开辟arr字符串（用于拷贝str字符串）
			strcpy(arr, str); //拷贝str给arr
			size_t left = 0, right = len - 1; //设置左右指针
			//逆置字符串arr
			while (left < right)
			{
				::swap(arr[left], arr[right]);
				left++;
				right--;
			}
			if (pos >= _size) //所给pos大于字符串有效长度
			{
				pos = _size - 1; //重新设置pos为字符串最后一个字符的下标
			}
			pos = _size - 1 - pos; //将pos改为镜像对称后的位置
			size_t ret = tmp.find(arr, pos); //复用find函数
			delete[] arr; //销毁arr指向的空间，避免内存泄漏
			if (ret != npos)
				return _size - ret - len; //找到了，返回ret镜像对称后再调整的位置
			else
				return npos; //没找到，返回npos
		}

		//关系运算符重载函数
		bool operator>(const String& s)const
		{
			return strcmp(_str, s._str) > 0;
		}
		bool operator>=(const String& s)const
		{
			return (*this > s) || (*this == s);
		}
		bool operator<(const String& s)const
		{
			return !(*this >= s);
		}
		bool operator<=(const String& s)const
		{
			return !(*this > s);
		}
		bool operator==(const String& s)const
		{
			return strcmp(_str, s._str) == 0;
		}
		bool operator!=(const String& s)const
		{
			return !(*this == s);
		}
		
	private:
		char* _str;//存储字符数
		size_t _size;//记录字符串当前的容量
		size_t _capacity;//记录字符串当前的容量
		static const size_t npos;//静态成员变量
	};
	const size_t String::npos = -1;

	//<<和>>运算符重载函数
	istream& operator>>(istream& in, String& s)
	{
		s.clear();
		char ch = in.get();
		while (ch != ' ' && ch != '\n')
		{
			s += ch;
			ch = in.get();
		}
		return in;
	}
	ostream& operator<<(ostream& out, const String& s)
	{
		for (auto e : s)
		{
			cout << e;
		}
		return out;
	}
	istream& getline(istream& in, String& s)
	{
		s.clear();
		char ch = in.get();
		while (ch != '\n')
		{
			s += ch;
			ch = in.get();
		}
		return in;
	}
	
}
using namespace STL;
using namespace std;
int main()
{
	String s = "12345";
	String s1(s);
	cout << s <<endl;
	cout<< s1<<endl;
	s[1]='0';
	s1=s;
	s.append("1");
	s1.push_back('a');
	
	cout << s <<endl;
	cout<< s1<<endl;
}
